package com.spring.definition;

import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;

public class DefinitionMain {

	public static void main(String[] args) {

		ClassPathResource resource = new ClassPathResource(
				"beandefine/application-beandefine1.xml");
		DefaultListableBeanFactory factory = new XmlBeanFactory(resource);

		DefinedPersonBean definedPersonBean = (DefinedPersonBean) factory
				.getBean("definedPersonBean");

		DefinedDogBean definedDogBean1 = (DefinedDogBean) factory
				.getBean("definedDogBean1");
		DefinedDogBean definedDogBean2 = (DefinedDogBean) factory
				.getBean("definedDogBean2");
		definedPersonBean.doMarkDogCallout();

		BeanDefinitionBuilder builder;
		builder = BeanDefinitionBuilder
				.rootBeanDefinition(DefinedDogBean.class);
		builder.addPropertyValue("name", "dog_new_3_injiava");
		
		factory.removeBeanDefinition("definedDogBean1");//remove bean define时，会将依赖definedDogBean1的beaan 如definedPersonBean 也从容器中删除。 
		factory.registerBeanDefinition("definedDogBean1",
				builder.getBeanDefinition());

		definedPersonBean.doMarkDogCallout();

		definedPersonBean = (DefinedPersonBean) factory
				.getBean("definedPersonBean");
		definedPersonBean.doMarkDogCallout();
		definedDogBean2 = (DefinedDogBean) factory
				.getBean("definedDogBean2");
	}

}
